package com.jintoufs.web.action.tour;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.QueryRule;
import com.jintoufs.dao.tour.TourRouteOrderDetailMapper;
import com.jintoufs.domain.account.AccountDetail;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.scene.SceneOrder;
import com.jintoufs.domain.scene.SceneOrderDetail;
import com.jintoufs.domain.shop.Shop;
import com.jintoufs.domain.tour.TourRouteOrder;
import com.jintoufs.domain.tour.TourRouteOrderDetail;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.account.AccountDetailService;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.service.divided.DividedService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.service.tour.TourRouteOrderService;
import com.jintoufs.util.date.DateUtils;
import com.jintoufs.web.action.common.JxlsHandler;
import com.jintoufs.web.action.refund.RefundAction;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.apache.commons.lang.StringUtils;
import org.apache.http.conn.routing.RouteInfo;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.name;

@Controller
@RequestMapping("tour/order")
public class TourRouteOrderAction extends SupportAction {
    @Resource
    private TourRouteOrderService tourRouteOrderService;
    @Resource
    private RefundAction refundAction;
    @Resource
    private DividedService dividedService;
    @Resource
    private CustomerService customerService;
    @Resource
    private ShopService shopService;
    @Resource
    private AccountDetailService accountDetailService;

    @RequestMapping("list")
    public String list(Model model, String code, String name, String startDate, String endDate) {
        model.addAttribute("code", code);
        model.addAttribute("name", name);
        model.addAttribute("startDate", startDate);
        model.addAttribute("endDate", endDate);
        return "tour/order/list";
    }

    @RequestMapping("query")
    @ResponseBody
    @SystemControllerLog(description = "获取路线订单列表")
    public JqgridPage query(int page,
                            int rows, String sidx,
                            String sord,
                            String filters, String code, String name, String startDate, String endDate,
                            @CurrentUser Customer customer) {
        QueryFilters filters_;
        PageHelper.startPage(page, rows);
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }

        if (StringUtils.isNotEmpty(code)) {
            QueryRule role = new QueryRule();
            role.setField("tro.id");
            role.setOp("cn");
            role.setData(code);
            filters_.getRules().add(role);
        }
        if (StringUtils.isNotEmpty(name)) {
            QueryRule role = new QueryRule();
            role.setField("cus.mobile_phone");
            role.setOp("cn");
            role.setData(name);
            filters_.getRules().add(role);
        }
        if (StringUtils.isNotEmpty(startDate)) {
            QueryRule role = new QueryRule();
            role.setField("tro.in_time");
            role.setOp("ge");
            role.setData(startDate);
            filters_.getRules().add(role);
        }
        if (StringUtils.isNotEmpty(endDate)) {
            QueryRule role = new QueryRule();
            role.setField("tro.in_time");
            role.setOp("le");
            role.setData(endDate);
            filters_.getRules().add(role);
        }

        filters_.setSidx(sidx);
        filters_.setSord(sord);
        QueryRule role = new QueryRule();
        role.setField("sm.customer_id");
        role.setOp("eq");
        role.setData(customer.getId());
        filters_.getRules().add(role);

        List<Map<String, Object>> orders = tourRouteOrderService.queryOrderByFilters(filters_);
        PageInfo pageInfo = new PageInfo(orders);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getList());
    }



    @RequestMapping("confirm")
    @ResponseBody
    @Transactional
    public AjaxMsg confirm(String id, @CurrentUser Customer customer) {
        AjaxMsg msg = new AjaxMsg();
        try {
            tourRouteOrderService.payConfirm(id);

            msg.setCode("200");
            msg.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(AjaxMsg.FAILURE);
        }

        return msg;
    }

    @RequestMapping(value = "/detail")
    public String detail(String id, Model model) {
        List<TourRouteOrderDetail> orderDetail = tourRouteOrderService.selectDetailByRouteOrderId(id);
        Map<String, Object> routeOrder = tourRouteOrderService.selectById(id);
        model.addAttribute("orderDetails", orderDetail);
        model.addAttribute("routeOrder", routeOrder);
        return "tour/order/detail";
    }


    @RequestMapping("verify")
    @Transactional
    @ResponseBody
    public AjaxMsg verify(String id) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        TourRouteOrder tourRouteOrder = tourRouteOrderService.getById(id);
        if (DateUtils.intervalDays(tourRouteOrder.getModifyTime(), new Date()) > 1 && tourRouteOrder.getStatus().equals(Constants.ORDER_STATUS_CONFIRM)) {
            dividedService.doDivided(tourRouteOrder,getUUID(),"tour");
        } else {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("当前订单状态不可核销，请检查订单已确认或订单处于确认状态已经一天！");
        }

        return ajaxMsg;


    }
}
